//
//  RCMoreVC.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMoreVC.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "Utility.h"
#import "RCAboutVC.h"
#import "RCCityListVC.h"
#import "RCFeedbackVC.h"
#import "RCSettingVC.h"
#import "GloableEngineService.h"
#import "RCAddMerchantViewController.h"

#define kSignalImageFile            @"image_more_signal.png"
#define kUpperBGImageFile           @"image_more_cellBG_upper.png"
#define kDownBGImageFile            @"image_more_cellBG_down.png"
#define kChageCityImageFile         @"image_more_changeCity.png"
#define kSettingImageFile           @"image_more_systemSetting.png"
#define kAddMerchantImageFile       @"image_more_addMerchant.png"
#define kFeedBackImageFile          @"image_more_feedBack.png"
#define kAboutImageFile             @"image_more_about.png"

#define kChageCityButtonFrame       CGRectMake(13,17,296,40)
#define kSettingButtonFrame         CGRectMake(13,57,296,40)
#define kAddMerchantButtonFrame     CGRectMake(13,97,296,40)
#define kFeedbackButtonFrame        CGRectMake(13,154,296,40)
#define kAboutButtonFrame           CGRectMake(13,194,296,40)

#define kButtonImageFrame           CGRectMake(14,12,16,16)
#define kButtonLabelFrame           CGRectMake(40,12,180,16)
#define kButtonDetailLabelFrame     CGRectMake(230,15,55,12)

#define kTagChageCity               101
#define kTagSetting                 102
#define kTagFeedback                103
#define kTagAbout                   104
#define kTagAddMerchant             105

@interface RCMoreVC ()

@property (nonatomic, retain)UILabel *changeCityDetailLabel;

@end

@implementation RCMoreVC

@synthesize changeCityDetailLabel = _changeCityDetailLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"更多";
    self.navigationItem.titleView = [self createTitleView];
    UIButton *signalButton = [UIButton buttonWithType:UIButtonTypeCustom];
    signalButton.frame = CGRectMake(0, 0, 44, 44);
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0) {
        signalButton.frame = CGRectMake(-10, 0, 44, 44);
    }
    [signalButton setImage:[UIImage imageNamed:kSignalImageFile]
                  forState:UIControlStateNormal];
    [signalButton setImage:[UIImage imageNamed:kSignalImageFile]
                  forState:UIControlStateHighlighted];
    self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:signalButton];
}

-(void)dealloc{
    self.changeCityDetailLabel = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //add chage city button
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    NSString *upperFilePath = [resourcePath stringByAppendingPathComponent:kUpperBGImageFile];
    NSString *downFilePath = [resourcePath stringByAppendingPathComponent:kDownBGImageFile];
    
    UIImage *upperImage = [UIImage imageWithContentsOfFile:upperFilePath];
    upperImage = [upperImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    
    UIImage *downImage = [UIImage imageWithContentsOfFile:downFilePath];
    downImage = [downImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    UIImage *pressImage = [Utility imageFromColor:[UIColor colorWithHex:0xffe6e6e6]];
    
    
    UIButton *changeCityButton = [[UIButton alloc]initWithFrame:kChageCityButtonFrame];
    changeCityButton.tag = kTagChageCity;
    [changeCityButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [changeCityButton setBackgroundImage:upperImage forState:UIControlStateNormal];
    [changeCityButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
    [changeCityButton setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    
    UIImageView *changeCityImageView = [[UIImageView alloc]initWithFrame:kButtonImageFrame];
    changeCityImageView.backgroundColor = [UIColor clearColor];
    UIImage *changeCityImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kChageCityImageFile]];
    [changeCityImageView setImage:changeCityImage];
    [changeCityButton addSubview:changeCityImageView];
    [changeCityImageView release];
    
    UILabel *changeCityLabel = [[UILabel alloc]initWithFrame:kButtonLabelFrame];
    changeCityLabel.backgroundColor = [UIColor clearColor];
    changeCityLabel.textColor = [UIColor colorWithHex:0xff505050];
    changeCityLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    changeCityLabel.text = @"切换城市";
    [changeCityButton addSubview:changeCityLabel];
    [changeCityLabel release];
    
    if (!_changeCityDetailLabel) {
        _changeCityDetailLabel = [[UILabel alloc]initWithFrame:kButtonDetailLabelFrame];
    }
    _changeCityDetailLabel.backgroundColor = [UIColor clearColor];
    _changeCityDetailLabel.textColor = [UIColor colorWithHex:0xff979797];
    _changeCityDetailLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _changeCityDetailLabel.textAlignment = UITextAlignmentRight;
    _changeCityDetailLabel.text = @"北京";
    [changeCityButton addSubview:_changeCityDetailLabel];
    
    [self.viewContainer addSubview:changeCityButton];
    [changeCityButton release];
    
    //add setting to view
    UIButton *settingButton = [[UIButton alloc]initWithFrame:kSettingButtonFrame];
    settingButton.tag = kTagSetting;
    [settingButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [settingButton setBackgroundImage:downImage forState:UIControlStateNormal];
    [settingButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
    [settingButton setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    
    UIImageView *settingImageView = [[UIImageView alloc]initWithFrame:kButtonImageFrame];
    settingImageView.backgroundColor = [UIColor clearColor];
    UIImage *settingImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kSettingImageFile]];
    [settingImageView setImage:settingImage];
    [settingButton addSubview:settingImageView];
    [settingImageView release];
    
    UILabel *settingLabel = [[UILabel alloc]initWithFrame:kButtonLabelFrame];
    settingLabel.backgroundColor = [UIColor clearColor];
    settingLabel.textColor = [UIColor colorWithHex:0xff505050];
    settingLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    settingLabel.text = @"系统设置";
    [settingButton addSubview:settingLabel];
    [settingLabel release];
    
    
    [self.viewContainer addSubview:settingButton];
    [settingButton release];
    
    
    //add merchant add
    
    UIButton *merchantAddButton = [[UIButton alloc]initWithFrame:kAddMerchantButtonFrame];
    merchantAddButton.tag = kTagAddMerchant;
    [merchantAddButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [merchantAddButton setBackgroundImage:downImage forState:UIControlStateNormal];
    [merchantAddButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
    [merchantAddButton setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    
    UIImageView *merchantAddImageView = [[UIImageView alloc]initWithFrame:kButtonImageFrame];
    merchantAddImageView.backgroundColor = [UIColor clearColor];
    UIImage *merchantAddImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kAddMerchantImageFile]];
    [merchantAddImageView setImage:merchantAddImage];
    [merchantAddButton addSubview:merchantAddImageView];
    [merchantAddImageView release];
    
    UILabel *merchantAddLabel = [[UILabel alloc]initWithFrame:kButtonLabelFrame];
    merchantAddLabel.backgroundColor = [UIColor clearColor];
    merchantAddLabel.textColor = [UIColor colorWithHex:0xff505050];
    merchantAddLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    merchantAddLabel.text = @"添加商户";
    [merchantAddButton addSubview:merchantAddLabel];
    [merchantAddLabel release];
    
    
    [self.viewContainer addSubview:merchantAddButton];
    [merchantAddButton release];
    
    //add feedback to view
    UIButton *feedbackButton = [[UIButton alloc]initWithFrame:kFeedbackButtonFrame];
    feedbackButton.tag = kTagFeedback;
    [feedbackButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [feedbackButton setBackgroundImage:upperImage forState:UIControlStateNormal];
    [feedbackButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
    [feedbackButton setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    
    UIImageView *feedbackImageView = [[UIImageView alloc]initWithFrame:kButtonImageFrame];
    feedbackImageView.backgroundColor = [UIColor clearColor];
    UIImage *feedbackImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kFeedBackImageFile]];
    [feedbackImageView setImage:feedbackImage];
    [feedbackButton addSubview:feedbackImageView];
    [feedbackImageView release];
    
    UILabel *feedbackLabel = [[UILabel alloc]initWithFrame:kButtonLabelFrame];
    feedbackLabel.backgroundColor = [UIColor clearColor];
    feedbackLabel.textColor = [UIColor colorWithHex:0xff505050];
    feedbackLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    feedbackLabel.text = @"意见反馈";
    [feedbackButton addSubview:feedbackLabel];
    [feedbackLabel release];
    
    
    [self.viewContainer addSubview:feedbackButton];
    [feedbackButton release];
    
    //add about to view
    UIButton *aboutButton = [[UIButton alloc]initWithFrame:kAboutButtonFrame];
    aboutButton.tag = kTagAbout;
    [aboutButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [aboutButton setBackgroundImage:downImage forState:UIControlStateNormal];
    [aboutButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
    [aboutButton setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    
    UIImageView *aboutImageView = [[UIImageView alloc]initWithFrame:kButtonImageFrame];
    aboutImageView.backgroundColor = [UIColor clearColor];
    UIImage *aboutImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kAboutImageFile]];
    [aboutImageView setImage:aboutImage];
    [aboutButton addSubview:aboutImageView];
    [aboutImageView release];
    
    UILabel *aboutLabel = [[UILabel alloc]initWithFrame:kButtonLabelFrame];
    aboutLabel.backgroundColor = [UIColor clearColor];
    aboutLabel.textColor = [UIColor colorWithHex:0xff505050];
    aboutLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    aboutLabel.text = @"关于我们";
    [aboutButton addSubview:aboutLabel];
    [aboutLabel release];
    
    
    [self.viewContainer addSubview:aboutButton];
    [aboutButton release];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *currentSelectCity = [GloableEngineService sharedInstance].selectedCityName;
    if (![_changeCityDetailLabel.text isEqualToString:currentSelectCity]) {
        _changeCityDetailLabel.text = currentSelectCity;
    }
}

-(void)buttonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
        case kTagChageCity:{
            RCCityListVC *cityListVC = [[RCCityListVC alloc] init];
            cityListVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cityListVC animated:YES];
            [cityListVC release];
            break;
        }
        case kTagSetting:{
            RCSettingVC *settingVC = [[RCSettingVC alloc]init];
            settingVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:settingVC animated:YES];
            [settingVC release];
            break;
        }
        case kTagFeedback:{
            RCFeedbackVC *feedbackVC = [[RCFeedbackVC alloc]init];
            feedbackVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:feedbackVC animated:YES];
            [feedbackVC release];
            break;
        }
            
        case kTagAbout:{
            RCAboutVC *aboutVC = [[RCAboutVC alloc]init];
            aboutVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutVC animated:YES];
            [aboutVC release];
            break;
        }
        case kTagAddMerchant:{
            RCAddMerchantViewController *vc = [[RCAddMerchantViewController alloc]init];
             vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];
            break;
        }
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
